﻿using System;
using log4net;

namespace MTC.Common
{
    public static class Logging
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
        
        public static void Start()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        public static void Debug(string message)
        {
            if (message == null) return;
            if (log.IsDebugEnabled)
            {
                log.Debug(message);
            }
        }

        public static void Info(string message)
        {
            if (message == null) return;
            if (log.IsInfoEnabled)
            {
                log.Info(message);
            }
        }

        public static void Warn(string message)
        {
            if (message == null) return;
            if (log.IsWarnEnabled)
            {
                log.Warn(message);
            }
        }

        public static void Error(string message,Exception ex)
        {
            if (message == null) return;
            if (log.IsErrorEnabled)
            {
                log.Error(message, ex);
            }
        }

        public static void Fatal(string message, Exception ex)
        {
            if (message == null) return;
            if (log.IsFatalEnabled)
            {
                log.Fatal(message, ex);
            }
        }
    
        public static void FlushAllAppenders()
        {
            foreach(log4net.Appender.IAppender appender in log4net.LogManager.GetRepository().GetAppenders())  
            {
                log4net.Appender.BufferingAppenderSkeleton buffer = appender as log4net.Appender.BufferingAppenderSkeleton;  
                if (buffer != null)
                {
                    buffer.Flush();
                }
            }
        }

        public static void Shutdown()
        {
            LogManager.Shutdown(); 
        }
    
    }
}